<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2014 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Forpastime <forpastime@qq.com>
// +----------------------------------------------------------------------
namespace plugins\aliyunvod\controller; 

use cmf\controller\PluginAdminBaseController;
use think\Db;
use think\db\Query;
use think\facade\Request;

class AdminIndexController extends PluginAdminBaseController
{

    public function _initialize()
    {
        $adminId = cmf_get_current_admin_id();//获取后台管理员id，可判断是否登录
        if (!empty($adminId)) {
            $this->assign("admin_id", $adminId);
        } else {
            $this->error('未登录');
        }
    }

    /**
     * 视频管理
     * @adminMenu(
     *     'name'   => '视频管理',
     *     'parent' => 'admin/Plugin/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '视频管理',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $param = $this->request->param();
        $isPage=false;
        $start_time = !empty($param['start_time'])?strtotime($param['start_time']):0;
        $end_time = !empty($param['end_time'])?strtotime($param['end_time']):strtotime('+1 day');
        
        if($start_time!=0){
            $video = Db::connect()->name('plugin_aliyunvod')
                ->where(function (Query $query) use ($param,$isPage ) {
                    $keyword = empty($param['keyword']) ? '' : $param['keyword'];
                    if (!empty($keyword)) {
                        $query->whereOr('title', 'like', "%$keyword%");
                    }
                })
                ->whereBetweenTime('create_time', $start_time, $end_time)
                ->where('status',1)
                ->order('create_time', 'DESC')
                ->paginate(10);
                // var_dump(date('Y-m-d',$end_time));
            $this->assign('start_time',date('Y-m-d',$start_time));
            $this->assign('end_time',date('Y-m-d',$end_time));
        }else{
            $video = Db::connect()->name('plugin_aliyunvod')
            ->where(function (Query $query) use ($param,$isPage ) {
                $keyword = empty($param['keyword']) ? '' : $param['keyword'];
                if (!empty($keyword)) {
                    $query->whereOr('title', 'like', "%$keyword%");
                }
            })
            ->where('status',1)
            ->order('create_time', 'DESC')
            ->paginate(10);
            $this->assign('end_time',date('Y-m-d',$end_time));
        }
        
        $video->appends($param);

        $this->assign('page', $video->render());
        $this->assign('video',$video);

        

        $cate = Db::connect()->name('plugin_aliyunvod_category')
            ->where('is_del',0)
            ->order('create_time', 'DESC')
            ->select()
            ->toArray();
        $this->assign('cate',$cate);

        return $this->fetch('/admin_index');
    }
    
}